from abc import abstractmethod
from algorithms.inferencer_base import InferencerBase


class InferencerBaseDetect(InferencerBase):
    def __init__(self, model_name, device='cuda'):
        super().__init__(model_name=model_name, device=device)
        self.device = device
        self.model = self._load_model(model_name)

    @abstractmethod
    def _load_model(self, model_name):
        """加载检测模型（如 YOLO、Faster R-CNN 等）"""
        pass

    @abstractmethod
    def preprocess(self, input_data):
        """对输入数据进行预处理（如缩放、归一化、格式转换等）"""
        pass

    @abstractmethod
    def postprocess(self, outputs):
        """对模型输出进行后处理（如解码 bounding box、NMS、置信度过滤等）"""
        pass

    def inference(self, input_data):
        """标准检测流程：预处理 -> 推理 -> 后处理"""
        preprocessed = self.preprocess(input_data)
        raw_output = self.model(preprocessed)
        result = self.postprocess(raw_output)
        return result

    def detect_image(self, image_path):
        """对单张图像进行检测"""
        # 可以在这里调用 preprocess 和 inference
        raise NotImplementedError("该方法尚未实现")

    def detect_batch(self, image_paths):
        """批量图像检测"""
        results = []
        for img in image_paths:
            result = self.detect_image(img)
            results.append(result)
        return results

    def visualize(self, image, detection_result):
        """可视化检测结果（如画 bounding box、标签等）"""
        raise NotImplementedError("该方法可用于可视化，但尚未实现")